<?php

namespace iBrand\Shop\Server\Controllers\V2;

use iBrand\Common\Controllers\Controller;
use iBrand\Shop\Server\Resources\GoodsResource;
use iBrand\Shop\Server\Resources\O2oGoodsResource;
use iBrand\Shop\Server\Service\GoodsService;

class GoodsController extends Controller
{
	protected $goodsService;

	public function __construct(GoodsService $goodsService)
	{
		$this->goodsService = $goodsService;
	}

	/**
	 * 根据商品名称或货号获取门店product数据
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function getProductsByCredentials()
	{
		$clerk    = auth('clerk')->user();
		$keyWord  = request('keyword');
		$products = $this->goodsService->getProductsByCredentials($clerk->shop_id, $keyWord);

		return $this->collection($products, O2oGoodsResource::class);
	}

	/**
	 * 根据门店商品ID获取sku数据
	 *
	 * @param $id
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function getStockByID($id)
	{
		$stock = $this->goodsService->getStockByGoodsID($id);

		return $this->success($stock);
	}


    /**
     * 根据SKU编码获取门店product数据
     * @return mixed
     */
	public function getProductByScanSku()
    {
        $clerk    = auth('clerk')->user();
        $keyWord  = request('sku');
        $products = $this->goodsService->getProductsBySku($clerk->shop_id, $keyWord);

        if(!$products) return $this->failed('商品不存在');

        return $this->item($products, O2oGoodsResource::class);
    }
}